<%@page import="com.google.gdata.data.photos.AlbumEntry"%>
<%@page import="com.bitzer.google.Picassa"%>
<%@page import="com.bitzer.google.Utils"%>
<%@page import="java.io.IOException"%>
<%@page import="com.google.gdata.util.ServiceException"%>
<%@page import="com.google.gdata.data.docs.DocumentListFeed"%>
<%@page import="com.google.gdata.data.docs.DocumentListEntry"%>
<%@page import="java.util.List"%>
<%@page import="com.bitzer.google.Constants"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <meta name="bitzer-component" content="data"/>
        <title>Picasa Album List</title>
    </head>
    <body>
        <%

                    String username = (String) session.getAttribute("username");
                    String password = (String) session.getAttribute("password");

                    if (username == null || username == ""
                            || password == null || password == "") {
                        response.sendRedirect(request.getContextPath()+"/login.jsp");
                    }

                    Picassa picassa = Picassa.instance();
                    picassa.authenticate(username, password);
                    String startRowStr = request.getParameter("startRow");
                    String numberOfRowsStr = request.getParameter("numberOfRows");
                    String lastModifiedOn = request.getParameter("lastModifiedOn");

                    int startRow = Utils.stringToInt(startRowStr, 1);
                    if (startRow == 0) {
                        startRow = 1;
                    }
                    int numberOfRows = Utils.stringToInt(numberOfRowsStr, 50000);
                    if (numberOfRows == 0) {
                        numberOfRows = 50000;
                    }
                    
                    List<AlbumEntry> feed = null;
                    try {

                        feed  = picassa.getFeed(lastModifiedOn, startRow, numberOfRows);

                     } catch (Exception e) 
                     {

                     }

        %>

        <table data-bitzer-data="true"
               data-bitzer-title="Picassa Albums"
               data-bitzer-desc="List of picassa albums"
               data-bitzer-component="Albums"
               data-bitzer-record-count="<%=picassa.recordCount%>"
               data-bitzer-start-index="<%=startRow%>"
               data-bitzer-number-of-rows="<%=numberOfRows%>">
            <tr>
                <th data-bitzer-field="id" data-bitzer-key-order="1">id</th>

                <th data-bitzer-field="title">Title</th>
                <th data-bitzer-field="thump">Thumbnail</th>
                <th data-bitzer-field="lastModifiedOn">Last Modified</th>
            </tr>
            <%
                        try {


                             if (!feed.isEmpty()) {

                                for (AlbumEntry entry : feed) {

                                    out.print(Utils.dataRowElement("id",entry.getGphotoId() ));
                                    out.print(Utils.dataCell(entry.getGphotoId()));
                                    out.print(Utils.dataCell(entry.getTitle().getPlainText()));
                                    out.print(Utils.dataCell(entry.getMediaThumbnails().get(0).getUrl()));
                                    out.print(Utils.dataCell(Utils.formattedDateTime(entry.getUpdated())));
                                    out.print("</tr>");
                                    
                                }
                            }

                            
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

            %>





        </table>

    </body>
</html>
